/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package alcatelparser.modules;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

/**
 *
 * @author geniuz
 */
public class Txt2BinConverter
{

    public Txt2BinConverter()
    {
    }

    public final void convertToBin(File file, File file1) throws FileNotFoundException, IOException
    {
        FileOutputStream fileoutputstream = null;
        //ProgressDialog progressdialog = (ProgressDialog)Env.getEnv().get(I.I(5156));
        //progressdialog.setProcessStatusText(I.I(7807));
        BufferedReader bufferedreader = new BufferedReader(new FileReader(file));
        fileoutputstream = new FileOutputStream(file1);
        _baos = new ByteArrayOutputStream();
        Object obj = null;
        int i = 0;
        boolean flag = false;
        int j = 0;
        do
        {
            String s;
            if((s = bufferedreader.readLine()) == null)
                break;
            if(s.contains(I.I(7826)))
                flag = true;
            if(isLineNeededToInclude(s, flag))
            {
                String s1 = s.substring(s.indexOf(I.I(7802)) + 1);
                decodeString(s1.trim(), s.contains(I.I(7826)));
                _baos.writeTo(fileoutputstream);
                _baos.reset();
                j++;
                i++;
                if(j > 1000)
                {
                    //progressdialog.setProcessStatusText((new StringBuilder()).append(I.I(7838)).append(i).append(I.I(7849)).toString());
                    j = 0;
                }
            } else
            if("".equals(s.trim()) && flag)
                flag = false;
        } while(true);
        _baos.close();
        fileoutputstream.flush();
        fileoutputstream.close();
        bufferedreader.close();
        if(fileoutputstream != null)
        {
            fileoutputstream.flush();
            fileoutputstream.close();
        }
        if(_baos != null)
        {
            _baos.flush();
            _baos.close();
        }
        //break MISSING_BLOCK_LABEL_349;
        //Exception exception;
        //exception;
        if(fileoutputstream != null)
        {
            fileoutputstream.flush();
            fileoutputstream.close();
        }
        if(_baos != null)
        {
            _baos.flush();
            _baos.close();
        }
        //throw exception;
    }

    private void decodeString(String s, boolean flag)
    {
        String as[] = s.split(I.I(4987));
        if(flag)
            setStartMeaningIndex(as);
        String as1[] = as;
        int i = as1.length;
        for(int j = 0; j < i; j++)
        {
            String s1 = as1[j];
            if(s1 == null || s1.length() != 4)
                continue;
            String s2 = (new StringBuilder()).append(s1.charAt(2)).append("").append(s1.charAt(3)).toString();
            if(isPutToFile(s2))
                _baos.write((char)Integer.parseInt(s2, 16));
            String s3 = (new StringBuilder()).append(s1.charAt(0)).append("").append(s1.charAt(1)).toString();
            if(isPutToFile(s3))
                _baos.write((char)Integer.parseInt(s3, 16));
        }
    }

    private void setStartMeaningIndex(String as[])
    {
        if(as.length == 0)
            return;
        if(isA3Tag(as))
            return;
        String s = as[1];
        int i = 1;
        String s1 = (new StringBuilder()).append("").append(s.charAt(2)).append(s.charAt(3)).toString();
        int j = Integer.parseInt(s1, 16);
        if(j > 128)
        {
            int k = j - 128;
            if(k > 0)
                i += k;
        }
        as[0] = null;
        int l = 0;
        do
        {
            if(i <= 0)
                break;
            l++;
            i -= 2;
            if(l < as.length)
            {
                String s2 = as[l];
                if(i == -1)
                {
                    String s3 = (new StringBuilder()).append("").append(s2.charAt(0)).append(s2.charAt(1)).toString();
                    _baos.write((char)Integer.parseInt(s3, 16));
                }
                as[l] = null;
            }
        } while(true);
    }

    private boolean isA3Tag(String as[])
    {
        if(as.length > 1)
        {
            String s = as[0];
            String s1 = (new StringBuilder()).append("").append(s.charAt(2)).append(s.charAt(3)).toString();
            return I.I(6953).equalsIgnoreCase(s1);
        } else
        {
            return false;
        }
    }

    private boolean isPutToFile(String s)
    {
        if(I.I(7804).equals(s))
            return false;
        try
        {
            Integer.parseInt(s, 16);
        }
        catch(NumberFormatException numberformatexception)
        {
            return false;
        }
        return true;
    }

    private boolean isLineNeededToInclude(String s, boolean flag)
    {
        return flag && s.contains(I.I(7802));
    }

    private ByteArrayOutputStream _baos;
    private static final String USER_BUFFER_FLAG = "USER_BUFFER";
    private static final String EMPTY_LINE = "";
    private static final String XX_NOT_PUT_VALUE = "XX";
}
